package com.example.onlineeducation.utils.pictureseletor

import android.content.Context
import android.graphics.Bitmap
import android.graphics.PointF
import android.view.View
import android.widget.ImageView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import com.luck.picture.lib.engine.ImageEngine
import com.luck.picture.lib.listener.OnImageCompleteCallback
import com.luck.picture.lib.tools.MediaUtils
import com.luck.picture.lib.widget.longimage.ImageSource
import com.luck.picture.lib.widget.longimage.ImageViewState
import com.luck.picture.lib.widget.longimage.SubsamplingScaleImageView
import com.yc.jetpacklib.extension.*

class YcGlideEngine private constructor() : ImageEngine {

    override fun loadImage(context: Context, url: String, imageView: ImageView) {
        imageView.ycLoadImageNet(url)
    }

    /**
     * 加载网络图片适配长图方案
     * 注意：此方法只有加载网络图片才会回调
     */
    override fun loadImage(context: Context, url: String, imageView: ImageView, longImageView: SubsamplingScaleImageView, callback: OnImageCompleteCallback) {
        imageView.ycLoadImageViewTarget(
            url,
            onLoadStartedCallBack = {
                callback.onShowLoading()
            },
            onLoadFailedCallBack = {
                callback.onHideLoading()
            },
            setResourceCallBack = { resource ->
                callback.onHideLoading()
                showImage(resource, imageView, longImageView)
            }
        )
    }


    override fun loadImage(context: Context, url: String, imageView: ImageView, longImageView: SubsamplingScaleImageView) {
        imageView.ycLoadImageViewTarget(
            url, setResourceCallBack = { resource ->
                showImage(resource, imageView, longImageView)
            }
        )
    }

    private fun showImage(resource: Bitmap?, imageView: ImageView, longImageView: SubsamplingScaleImageView) {
        if (resource != null) {
            val eqLongImage = MediaUtils.isLongImg(resource.width, resource.height)
            longImageView.visibility = if (eqLongImage) View.VISIBLE else View.GONE
            imageView.visibility = if (eqLongImage) View.GONE else View.VISIBLE
            if (eqLongImage) { // 加载长图
                longImageView.isQuickScaleEnabled = true
                longImageView.isZoomEnabled = true
                longImageView.isPanEnabled = true
                longImageView.setDoubleTapZoomDuration(100)
                longImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP)
                longImageView.setDoubleTapZoomDpi(SubsamplingScaleImageView.ZOOM_FOCUS_CENTER)
                longImageView.setImage(ImageSource.bitmap(resource), ImageViewState(0f, PointF(0f, 0f), 0))
            } else { // 普通图片
                imageView.setImageBitmap(resource)
            }
        }
    }


    override fun loadFolderImage(context: Context, url: String, imageView: ImageView) {
        imageView.ycLoadBitmapImageViewTarget(url, 180, 180, 0.5f) { resource ->
            val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.resources, resource)
            circularBitmapDrawable.cornerRadius = 8f
            imageView.setImageDrawable(circularBitmapDrawable)
        }
    }

    override fun loadAsGifImage(context: Context, url: String, imageView: ImageView) {
        imageView.ycLoadAsGifImage(url)
    }

    /**
     * 加载图片列表图片
     *
     * @param context   上下文
     * @param url       图片路径
     * @param imageView 承载图片ImageView
     */
    override fun loadGridImage(context: Context, url: String, imageView: ImageView) {
        imageView.ycLoadGridImage(url, 200, 200)
    }

    companion object {
        private var instance: YcGlideEngine? = null
        fun createGlideEngine(): YcGlideEngine? {
            if (null == instance) {
                synchronized(YcGlideEngine::class.java) {
                    if (null == instance) {
                        instance = YcGlideEngine()
                    }
                }
            }
            return instance
        }
    }
}